package com.example.demo.service.impl;

import com.example.demo.entity.Classes;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.util.PageRequest;
import com.example.demo.util.PageResult;
import com.example.demo.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    public Integer getUserCount(Map<String, Integer> map){
        return userMapper.getUserCount(map);
    }

    public Classes getClass3(int id){
        return userMapper.getClass3(id);
    }

    public Classes getClass(int id){
        return userMapper.getClass(id);
    }

    public User getUser(int id){
        return userMapper.getUser(id);
    }


    public void addUser(User user){
        userMapper.addUser(user);
    }


    public void delUser(int id){
        userMapper.delUser(id);
    }


    public void updateUser(User user){
        userMapper.updateUser(user);
    }


    public List<User> getAllUser(){
        return userMapper.getAllUser();
    }

    public List<User> getAllUserLike(String name){
        return userMapper.getAllUserLike(name);
    }

    public void addUserByAn(User user){
        userMapper.addUserByAn(user);
    }

    public PageResult findPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    @Override
    public List<User> findUser(User user) {
        int pageNum = 1;
        int pageSize = 2;
        PageHelper.startPage(pageNum, pageSize);
        List<User> user1 = userMapper.findUser(user);
        return user1;
    }

    /**
     * 调用分页插件完成分页
     * @param pageRequest
     * @return
     */
    private PageInfo<User> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<User> sysMenus = userMapper.getAllUser();
        return new PageInfo<User>(sysMenus);
    }
}
